import 'package:flutter/material.dart';
import 'dart:async';
import 'package:hairen_app/pages/plug/func.dart';
import 'package:hairen_app/server/server.dart';
import 'package:flutter/cupertino.dart';
import 'login.dart';
class Register extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _RegisterState() ;
  }
}

class _RegisterState extends State<Register>{
  TextEditingController _usernameCpntroller=new TextEditingController();
  TextEditingController _passwordCpntroller=new TextEditingController();
  TextEditingController _repasswordCpntroller=new TextEditingController();

  bool isSubmit=true;
  @override
  initState(){
    super.initState();
  }
  @override
  dispose(){
    super.dispose();
  }
  void onTextClear() {
    setState(() {
      _usernameCpntroller.text = "";
      _passwordCpntroller.text = "";
      _repasswordCpntroller.text = "";
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        elevation: 0.0,
        title: new Text("注册账号"),
      ),
      body:Align(
        alignment: Alignment.center,
        child: new Container(
          padding: EdgeInsets.only(left: 25.0,right: 25.0,top: 10.0,bottom: 10.0),
          color: Theme.of(context).backgroundColor,
          child: new Form(
            child: new Column(
              children: <Widget>[
                new SizedBox(height: 30.0,),
                new Image.asset(
                  'assets/logo.png',
                  width: 80.0,
                ),
                new SizedBox(height: 20.0,),
                new Container(
                  padding: EdgeInsets.only(left: 10.0,right: 10.0),
                  color: Colors.white,
                  child: new TextField(
                    controller: _usernameCpntroller,
                    decoration: new InputDecoration(
                      icon: new Icon(Icons.perm_identity,color: Theme.of(context).backgroundColor,),
                      border: InputBorder.none,
                      hintText: "请输入手机号或者邮箱",
                    ),
                    maxLines: 1,
//                       maxLength: 30,
                  ),
                ),
                new SizedBox(height: 20.0,),
                new Container(
                  padding: EdgeInsets.only(left: 10.0,right: 10.0),
                  color: Colors.white,
                  child: new TextField(
                    controller: _passwordCpntroller,
                    decoration: new InputDecoration(
                      icon: new Icon(Icons.lock_outline,color: Theme.of(context).backgroundColor,),
                      border: InputBorder.none,
                      hintText: "请输入密码",

                    ),
                    obscureText: true,
                    maxLines: 1,
//                       maxLength: 30,
                  ),
                ),
                new SizedBox(height: 20.0,),
                new Container(
                  padding: EdgeInsets.only(left: 10.0,right: 10.0),
                  color: Colors.white,
                  child: new TextField(
                    controller: _repasswordCpntroller,
                    decoration: new InputDecoration(
                      icon: new Icon(Icons.lock_outline,color: Theme.of(context).backgroundColor,),
                      border: InputBorder.none,
                      hintText: "请再次输入密码",

                    ),
                    obscureText: true,
                    maxLines: 1,
//                       maxLength: 30,
                  ),
                ),
                new SizedBox(height: 20.0,),
                new Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new Expanded(
                      flex: 1,
                      child:  new FlatButton(onPressed: (){
                      }, child: new Text('注册账号',style: TextStyle(color: Colors.white),)),
                    ),
                    new Expanded(
                      flex: 1,
                      child: new Builder(builder: (BuildContext context){
                        return   new FlatButton(
                            color: Colors.white,
                            onPressed: (){
                              if(isSubmit){
                                setState(() {
                                  isSubmit=false;
                                });
                                String username=_usernameCpntroller.text.trim();
                                String password=_passwordCpntroller.text.trim();
                                String repassword=_repasswordCpntroller.text.trim();
                                if(username==""){
                                  func.MySnackBar(context,'用户名不能为空',color: Colors.red);
                                  setState(() {
                                    isSubmit=true;
                                  });

                                }else if(password==""){
                                  func.MySnackBar(context,'密码不能为空',color: Colors.red);
                                  setState(() {
                                    isSubmit=true;
                                  });

                                } else if(password!=repassword){
                                  func.MySnackBar(context,'两次密码不一致',color: Colors.red);
                                  setState(() {
                                    isSubmit=true;
                                  });

                                }else{
                                  Server.register(data: {"username":username,"password":password}).then((ret){
                                    setState(() {
                                      isSubmit=true;
                                    });
                                    if(ret['code']==0){
                                      func.MySnackBar(context,ret['msg'],color: Colors.red);
                                    }else{
                                      func.MySnackBar(context,ret['msg'],color: Colors.white);
                                      Timer.periodic(Duration(seconds:1 ), (r){
                                        Navigator.of(context).push(
                                            new CupertinoPageRoute(builder: (context)=>new Login())
                                        );
                                        r.cancel();
                                      });
                                    }
                                  });
                                }

                              }

                            }, child: new Text('登陆',style: TextStyle(color: Theme.of(context).backgroundColor)));
                      })
                    )
                  ],
                )
              ],
            ),
          ),
        ),
      ),
      resizeToAvoidBottomPadding: false, //输入框抵住键盘
    );
  }
}